define(function(require){

	var emotionData = null;
	var s= {};
	
	var sourcePath = "widget://res/room/emotion";//表情存放目录
	
	
	function getImgsPaths(sourcePathOfChatBox, callback){
	
	    var jsonPath = sourcePathOfChatBox+"/emotion.json";//表情的JSON数组
	    var htmlpath = "../res/room/emotion";
	    api.readFile({
	            path: jsonPath
	    },function(ret,err){
	        if(ret.status){
	            var emotionArray = JSON.parse(ret.data);
	            var emotion = {};
	            for(var idx in emotionArray){
	                    var emotionItem = emotionArray[idx];
	                    var emotionText = emotionItem["text"];
	                    var emotionUrl = htmlpath+"/"+emotionItem["name"]+".png";
	                    emotion[emotionText] = emotionUrl;
	            }
	            /*把emotion对象 回调出去*/
	            if("function" === typeof(callback)){
	                    callback(emotion);
	            }
	        }
	    });
	}	
	

		
	getImgsPaths(sourcePath, function (emotion) {
	    emotionData = emotion;
	});
	
	
	s.transText = function(text, imgWidth, imgHeight){
	    var imgWidth = imgWidth || "1rem";
	    var imgHeight = imgHeight || "1rem";
	    var regx = /\[(.*?)\]/gm;
	    var textTransed = text.replace(regx,function(match){
	        var imgSrc = emotionData[match];
	        if(!imgSrc){
	                //说明不对应任何表情，直接返回
	                return match;
	        }
	        var img = '<img style="display:inline;width:'+imgWidth+';height:'+imgHeight+';" src="'+imgSrc+'" />';
	    
	        return img;
	    });
	    
	    return textTransed;
	}

	return s;
});